package com.bestseller.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collection;

public class ProxyMain {
	public static void main(String[] args) {
	
		final ArrayList target=new ArrayList();
		
		Collection proxy=(Collection) getProxy(target,new MyAdviceImpl());
		proxy.add("xgx");
		System.out.println(proxy.size());
		
	}

	private static Object getProxy(final Object target,final MyAdvice advice) {
		Object proxy=Proxy.newProxyInstance(target.getClass().getClassLoader(),
				target.getClass().getInterfaces(), 
				new InvocationHandler(){
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				advice.before(method);
				Object invoke = method.invoke(target, args);
				advice.after(method);
				return invoke;
			}
			
		});
		
		return proxy;
	}
	
	
	
}
